import Vue from "vue";
import VueRouter from "vue-router";
import { getToken } from "@/utils/token";

const home = () => import( /* webpackChunkName: "login_home_welome" */ '../views/home/index.vue')
const mylife = () => import( /* webpackChunkName: "login_home_welome" */ '../views/mylife/index')
const arrange = () => import( /* webpackChunkName: "login_home_welome" */ '../views/arrange/index')
const about = () => import( /* webpackChunkName: "login_home_welome" */ '../views/about/index')
const comment = () => import( /* webpackChunkName: "login_home_welome" */ '../views/comment/index')
const detail = () => import( /* webpackChunkName: "login_home_welome" */ '../views/detail/index')
const navigation = () => import( /* webpackChunkName: "login_home_welome" */ '../views/navigation/index')

Vue.use(VueRouter);

const routes = [
  {
    path: "*", //访问路由以外路径
    redirect: "/", //地址重定向到/home
  },
  {
    path: "/", //路径
    meta: {
      keepAlive: true, // 需要被缓存
    },
    component: home, //路径页面
  },
  {
    path: "/mylife", //路径
    component: mylife, //路径页面
  },
  {
    path: "/arrange", //路径
    meta: {
      keepAlive: true, // 需要被缓存
    },
    component: arrange, //路径页面
  },
  {
    path: "/about", //路径
    meta: {
      keepAlive: true, // 需要被缓存
    },
    component: about, //路径页面
  },
  {
    path: "/comment", //路径
    component: comment, //路径页面
  },
  {
    path: "/detail/:id", //路径
    component: detail, //路径页面
  },
  {
    path: "/navigation", //路径
    meta: {
      keepAlive: true, // 需要被缓存
    },
    component: navigation, //路径页面
  },
];

const router = new VueRouter({
  mode: "hash",
  // mode: 'history',
  base: process.env.BASE_URL,
  scrollBehavior: () => ({
    y: 0,
  }),
  routes,
});

// 防止连续点击多次路由报错
let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((err) => err);
};

export default router;
